<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="简历ID" prop="recResId">
      <el-input v-model="dataForm.recResId" placeholder="简历ID"></el-input>
    </el-form-item>
    <el-form-item label="HR_ID" prop="recHrId">
      <el-input v-model="dataForm.recHrId" placeholder="HR_ID"></el-input>
    </el-form-item>
    <el-form-item label="面试记录" prop="recImpression">
      <el-input v-model="dataForm.recImpression" placeholder="面试记录"></el-input>
    </el-form-item>
    <el-form-item label="记录时间" prop="recTime">
      <el-input v-model="dataForm.recTime" placeholder="记录时间"></el-input>
    </el-form-item>
    <el-form-item label="评分" prop="recScore">
      <el-input v-model="dataForm.recScore" placeholder="评分"></el-input>
    </el-form-item>
    <el-form-item label="备注1" prop="recBz1">
      <el-input v-model="dataForm.recBz1" placeholder="备注1"></el-input>
    </el-form-item>
    <el-form-item label="备注2" prop="recBz2">
      <el-input v-model="dataForm.recBz2" placeholder="备注2"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          recId: 0,
          recResId: '',
          recHrId: '',
          recImpression: '',
          recTime: '',
          recScore: '',
          recBz1: '',
          recBz2: ''
        },
        dataRule: {
          recResId: [
            { required: true, message: '简历ID不能为空', trigger: 'blur' }
          ],
          recHrId: [
            { required: true, message: 'HR_ID不能为空', trigger: 'blur' }
          ],
          recImpression: [
            { required: true, message: '面试记录不能为空', trigger: 'blur' }
          ],
          recTime: [
            { required: true, message: '记录时间不能为空', trigger: 'blur' }
          ],
          recScore: [
            { required: true, message: '评分不能为空', trigger: 'blur' }
          ],
          recBz1: [
            { required: true, message: '备注1不能为空', trigger: 'blur' }
          ],
          recBz2: [
            { required: true, message: '备注2不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.recId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.recId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/record/info/${this.dataForm.recId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.recResId = data.record.recResId
                this.dataForm.recHrId = data.record.recHrId
                this.dataForm.recImpression = data.record.recImpression
                this.dataForm.recTime = data.record.recTime
                this.dataForm.recScore = data.record.recScore
                this.dataForm.recBz1 = data.record.recBz1
                this.dataForm.recBz2 = data.record.recBz2
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/record/${!this.dataForm.recId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'recId': this.dataForm.recId || undefined,
                'recResId': this.dataForm.recResId,
                'recHrId': this.dataForm.recHrId,
                'recImpression': this.dataForm.recImpression,
                'recTime': this.dataForm.recTime,
                'recScore': this.dataForm.recScore,
                'recBz1': this.dataForm.recBz1,
                'recBz2': this.dataForm.recBz2
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
